package com.compus_help.controller.admin;


import com.compus_help.common.PageResult;
import com.compus_help.common.Result;
import com.compus_help.entity.DTO.adminNoticeDTO;
import com.compus_help.entity.DTO.adminNoticePageDTO;
import com.compus_help.entity.Pojo.Notice;
import com.compus_help.service.adminNoticeServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class adminNoticeController {
    //因为slf4j注解中使用log无法通过编译，使用这里使用loggerFactory获取logger
    //TODO csdn
    private static final Logger log = LoggerFactory.getLogger(adminNoticeController.class);

    @Autowired
    private adminNoticeServer adminNoticeServer;

    //公告分页查询
    @PostMapping("/notice/page")
    public Result<PageResult> getNoticePage(@RequestBody  adminNoticePageDTO adminNoticePageDTO) {
        //在控制台输出生效时间和失效时间
        System.out.println(adminNoticePageDTO.getStartTime());
        System.out.println(adminNoticePageDTO.getEndTime());
        PageResult pageResult = adminNoticeServer.getNoticePage(adminNoticePageDTO);
        log.info("分页查询公告成功");
        return Result.success(pageResult);
    }

    // 添加公告
    @PostMapping("/addnotice")
    public Result addNotice(@RequestBody adminNoticeDTO adminNoticeDTO) {
        //输出生失效时间
        log.info("添加公告生效时间参数: {}", adminNoticeDTO.getEffectiveTime());
        log.info("添加公告失效时间参数: {}", adminNoticeDTO.getExpireTime());
        System.out.println(adminNoticeDTO.getExpireTime());

        adminNoticeServer.addNotice(adminNoticeDTO);
        log.info("添加公告成功");
        return Result.success();
    }
    // 修改公告
    @PutMapping("/updatenotice")
    public Result updateNotice(@RequestBody adminNoticeDTO adminNoticeDTO) {
        adminNoticeServer.updateNotice(adminNoticeDTO);
        log.info("修改公告成功");
        return Result.success();
    }
    // 删除公告
    @DeleteMapping("/deletenotice")
    public Result deleteNotice(@RequestParam List<Integer> noticeId) {
        adminNoticeServer.deleteNotice(noticeId);
        log.info("删除公告成功，公告IDs: {}", noticeId);
        return Result.success();
    }
    // 修改公告状态
    // 修改公告状态
    @PostMapping("/notice/status/{status}")
    public Result updateNoticeStatus(@PathVariable String status, @RequestParam("noticeId") Integer noticeId) {
        // 创建更新对象
        Notice notice = new Notice();
        notice.setNoticeId(noticeId);
        notice.setStatus(status);
        adminNoticeServer.updateNoticeStatus(notice);
        log.info("修改公告状态成功，公告ID: {}，状态: {}", noticeId, status);
        return Result.success();
    }



}
